home *** CD-ROM | disk | FTP | other *** search
/ PC World Interactive 9 / PC World Interactive 9 - Temmuz 1998.iso / share / internet / hot / cgi.z / search.pl < prev    next >
Perl Script  |  1996-04-17  |  5KB  |  174 lines

  1. #!/bin/perl
  2. #############################################################################
  3. # Simple Search Script                                #
  4. # Written By Matt Wright                            #
  5. # Created on: 12/16/95  Last Modified on: 12/16/95                #
  6. # Version 1.0                                    #
  7. # I can be reached at:          mattw@misha.net                    #
  8. # Scripts found at:             http://www.worldwidemart.com/scripts/        #
  9. #############################################################################
  10. # Define Variables                                #
  11.  
  12. $basedir = '/export/home/youruserid/public_html/';
  13. $baseurl = 'http://yourserver/youruserid/';
  14. @files = ('*.htm');
  15. $title = "YOUR TITLE";
  16. $title_url = 'http://yourserver/youruserid/';
  17. $search_url = 'http://yourserver/youruserid/search.htm';
  18.  
  19. # Done                                        #
  20. #############################################################################
  21.  
  22. # Parse Form Search Information
  23. &parse_form;
  24.  
  25. # Get Files To Search Through
  26. &get_files;
  27.  
  28. # Search the files
  29. &search;
  30.  
  31. # Print Results of Search
  32. &return_html;
  33.  
  34.  
  35. sub parse_form {
  36.  
  37.    # Get the input
  38.    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  39.  
  40.    # Split the name-value pairs
  41.    @pairs = split(/&/, $buffer);
  42.  
  43.    foreach $pair (@pairs) {
  44.       ($name, $value) = split(/=/, $pair);
  45.  
  46.       $value =~ tr/+/ /;
  47.       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  48.  
  49.       $FORM{$name} = $value;
  50.    }
  51. }
  52.  
  53. sub get_files {
  54.  
  55.    chdir($basedir);
  56.    foreach $file (@files) {
  57.       $ls = `ls $file`;
  58.       @ls = split(/\s+/,$ls);
  59.       foreach $temp_file (@ls) {
  60.          if (-d $file) {
  61.             $filename = "$file$temp_file";
  62.             if (-T $filename) {
  63.                push(@FILES,$filename);
  64.             }
  65.          }
  66.          elsif (-T $temp_file) {
  67.             push(@FILES,$temp_file);
  68.          }
  69.       }
  70.    }
  71. }
  72.  
  73. sub search {
  74.  
  75.    @terms = split(/\s+/, $FORM{'terms'});
  76.  
  77.    foreach $FILE (@FILES) {
  78.  
  79.       open(FILE,"$FILE");
  80.       @LINES = <FILE>;
  81.       close(FILE);
  82.  
  83.       $string = join(' ',@LINES);
  84.       $string =~ s/\n//g;
  85.       if ($FORM{'boolean'} eq 'AND') {
  86.          foreach $term (@terms) {
  87.             if ($FORM{'case'} eq 'Insensitive') {
  88.                if (!($string =~ /$term/i)) {
  89.                   $include{$FILE} = 'no';
  90.             last;
  91.                }
  92.                else {
  93.                   $include{$FILE} = 'yes';
  94.                }
  95.             }
  96.             elsif ($FORM{'case'} eq 'Sensitive') {
  97.                if (!($string =~ /$term/)) {
  98.                   $include{$FILE} = 'no';
  99.                   last;
  100.                }
  101.                else {
  102.                   $include{$FILE} = 'yes';
  103.                }
  104.             }
  105.          }
  106.       }
  107.       elsif ($FORM{'boolean'} eq 'OR') {
  108.          foreach $term (@terms) {
  109.             if ($FORM{'case'} eq 'Insensitive') {
  110.                if ($string =~ /$term/i) {
  111.                   $include{$FILE} = 'yes';
  112.                   last;
  113.                }
  114.                else {
  115.                   $include{$FILE} = 'no';
  116.                }
  117.             }
  118.             elsif ($FORM{'case'} eq 'Sensitive') {
  119.                if ($string =~ /$term/) {
  120.           $include{$FILE} = 'yes';
  121.                   last;
  122.                }
  123.                else {
  124.                   $include{$FILE} = 'no';
  125.                }
  126.             }
  127.          }
  128.       }
  129.       if ($string =~ /<title>(.*)<\/title>/i) {
  130.          $titles{$FILE} = "$1";
  131.       }
  132.       else {
  133.          $titles{$FILE} = "$FILE";
  134.       }
  135.    }
  136. }
  137.       
  138. sub return_html {
  139.    print "Content-type: text/html\n\n";
  140.    print "<html>\n <head>\n  <title>Results of Search</title>\n </head>\n";
  141.    print "<body>\n <center>\n  <h1>Results of Search in $title</h1>\n </center>\n";
  142.    print "Below are the results of your Search in no particular order:<p><hr size=7 width=75%><p>\n";
  143.    print "<ul>\n";
  144.    foreach $key (keys %include) {
  145.       if ($include{$key} eq 'yes') {
  146.          print "<li><a href=\"$baseurl$key\">$titles{$key}</a>\n";
  147.       }
  148.    }
  149.    print "</ul>\n";
  150.    print "<hr size=7 width=75%>\n";
  151.    print "Search Information:<p>\n";
  152.    print "<ul>\n";
  153.    print "<li><b>Terms:</b> ";
  154.    $i = 0;
  155.    foreach $term (@terms) {
  156.       print "$term";
  157.       $i++;
  158.       if (!($i == @terms)) {
  159.          print ", ";
  160.       }
  161.    }
  162.    print "\n";
  163.    print "<li><b>Boolean Used:</b> $FORM{'boolean'}\n";
  164.    print "<li><b>Case $FORM{'case'}</b>\n";
  165.    print "</ul><br><hr size=7 width=75%><P>\n";
  166.    print "<ul>\n<li><a href=\"$search_url\">Back to Search Page</a>\n";
  167.    print "<li><a href=\"$title_url\">$title</a>\n";
  168.    print "</ul>\n";
  169.    print "<hr size=7 width=75%>\n";
  170.    print "Search Script written by Matt Wright and can be found at <a href=\"http://www.worldwidemart.com/scripts/\">Matt's Script Archive</a>\n";
  171.    print "</body>\n</html>\n";
  172. }
  173.    
  174.